package com.sam.im.heiqun.uis.adapters;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.google.gson.Gson;
import com.sam.im.heiqun.R;
import com.sam.im.heiqun.entities.ColactionEntity;
import com.sam.im.heiqun.entities.MsgEntity;
import com.sam.im.heiqun.entities.VedioEntity;
import com.sam.im.heiqun.uis.activities.ColactionDetailActivity;
import com.sam.im.heiqun.uis.adapters.holder.ColactionBaseViewHolder;
import com.sam.im.heiqun.uis.adapters.holder.ColatImgViewHolder;
import com.sam.im.heiqun.uis.adapters.holder.ColatTextViewHolder;
import com.sam.im.heiqun.uis.adapters.holder.ColatVideoViewHolder;
import com.sam.im.heiqun.uis.adapters.holder.ColatVoiceViewHolder;
import com.sam.im.heiqun.utils.IMMessageUtil;
import com.yuyh.library.utils.GlideUtils;
import com.yuyh.library.utils.ScreenUtil;
import com.yuyh.library.utils.TimeUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by leijiawen on 2017/6/19.
 */
public class ColactionAdapter extends BaseRecycleViewAdapter {

    /* 1文本2图片3语音4视频5表情6链接文章*/
    private Context context;
    private LayoutInflater inflater;
    private List<ColactionEntity.ListBean> datas = new ArrayList<>();
    private OnlongClickListenler onlongClickListenler;
    private OnCollecClickListenler onCollecClickListenler;

    public ColactionAdapter(Context context, List<ColactionEntity.ListBean> datas) {
        this.context = context;
        this.datas = datas;
        this.inflater = LayoutInflater.from(context);
    }

    public void setOnlongClickListenler(OnlongClickListenler onlongClickListenler) {
        this.onlongClickListenler = onlongClickListenler;
    }

    public void setOnCollecClickListenler(OnCollecClickListenler onCollecClickListenler) {
        this.onCollecClickListenler = onCollecClickListenler;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        RecyclerView.ViewHolder holder = null;
        View view = inflater.inflate(R.layout.layout_colactionbaseholder, parent, false);
        switch (viewType) {
            case 1://文字
                holder = new ColatTextViewHolder(view, 1);
                break;
            case 2://图片
                holder = new ColatImgViewHolder(view, 2);
                break;
            case 3://语音
                holder = new ColatVoiceViewHolder(view, 3);
                break;
            case 4://视频
                holder = new ColatVideoViewHolder(view, 4);
                break;
            case 5:
                holder = new ColatVideoViewHolder(view, 5);
                break;
            case 6:
                holder = new ColatVideoViewHolder(view, 6);
                break;
        }
        return holder;
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        ColactionEntity.ListBean listBean = datas.get(position);
        if (listBean != null) {
            String fromHeadUrl = listBean.getFromHeadUrl();
            String fromName = listBean.getFromName();
            String createTime = listBean.getCreateTime();
            final String content = listBean.getContent();
            final ColactionBaseViewHolder holder1 = (ColactionBaseViewHolder) holder;
            if (position == 0) {
                holder1.dividerview.setVisibility(View.VISIBLE);
            } else {
                holder1.dividerview.setVisibility(View.GONE);
            }
            holder1.backlayout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onlongClickListenler.longClick(holder1.backlayout, position);
                    return true;
                }
            });
            holder1.fromName.setText(fromName);
            String recentlyTime = TimeUtil.getRecentlyTime(Long.parseLong(createTime));
            holder1.tvTime.setText(recentlyTime);
//            Glide.with(context).load(fromHeadUrl).into(holder1.headIcon);
            GlideUtils.loadCircleImage(context, fromHeadUrl, holder1.headIcon);
            Log.i("fanhui","fromHeadUrl:"+fromHeadUrl);
            holder1.backlayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    onCollecClickListenler.onClick(v, position);

//                    switch (getItemViewType(position)){
//                        case 1://文字
//
//                            break;
//                        case 2://图片
//                            List<String> imgs = new ArrayList<String>();
//                            imgs.add(content);
//                            ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(v.getMeasuredWidth(), v.getMeasuredHeight());
//                            ImagePagerActivity.startImagePagerActivity(((CollectionActivity) context), imgs,0, imageSize);
//                            break;
//                        case 3://语音
//                            try {
//                                JSONObject obj = new JSONObject(content);
//                                int time = obj.getInt("time");
//                                final String url = obj.getString("url");
//                                ((ColatVoiceViewHolder)holder).tvTime.setText(time+"\"");
//                            ((ColatVoiceViewHolder)holder).tvTime.setVisibility(View.GONE);
//                            MediaManager.playSound(context, url, new MediaPlayer.OnCompletionListener() {
//                                @Override
//                                public void onCompletion(MediaPlayer mediaPlayer) {
//                                    ((ColatVoiceViewHolder)holder).tvTime.setVisibility(View.VISIBLE);
//                                }
//                            }, AudioManager.STREAM_MUSIC);
//                            } catch (JSONException e) {
//                                e.printStackTrace();
//                            }
//                            break;
//                        case 4://视频
//                            final VedioEntity vedioEntity = new Gson().fromJson(content,VedioEntity.class);
//                            Intent intent = new Intent(context, PlayActivity.class);
//                            intent.putExtra("videoUrl",vedioEntity.getVedioUrl());
//                            context.startActivity(intent);
//                            break;
//                        case 5:
//                            break;
//                        case 6:
//                            break;
//                    }
                }
            });
            switch (getItemViewType(position)) {
                case 1://文字
                    setTextlayout(holder, content, position, holder1.backlayout);
                    break;
                case 2://图片
                    setImglayout((ColatImgViewHolder) holder, content, position);
                    break;
                case 3://语音
                    setVoicelayout((ColatVoiceViewHolder) holder, content, position);
                    break;
                case 4://视频
                    setVideolayou((ColatVideoViewHolder) holder, content, position);
                    break;
                case 5:
                    setVideolayou((ColatVideoViewHolder) holder, content, position);
                    break;
                case 6:
                    setVideolayou((ColatVideoViewHolder) holder, content, position);
                    break;
            }
        }

    }

    private void setVoicelayout(final ColatVoiceViewHolder holder, String content, final int position) {
        try {
            JSONObject obj = new JSONObject(content);
            int time = obj.getInt("time");
            final String url = obj.getString("url");
            holder.tvTime.setText(time + "\"");
            /*holder.voice_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    holder.tvTime.setVisibility(View.GONE);
                    MediaManager.playSound(context, url, new MediaPlayer.OnCompletionListener() {
                        @Override
                        public void onCompletion(MediaPlayer mediaPlayer) {
                            holder.tvTime.setVisibility(View.VISIBLE);
                        }
                    }, AudioManager.STREAM_MUSIC);
                }
            });*/
           /* holder.voice_layout.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onlongClickListenler.longClick(holder.voice_layout,position);
                    return true;
                }
            });*/
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    private void setVideolayou(final ColatVideoViewHolder holder, String content, final int position) {
        try {
            final VedioEntity vedioEntity = new Gson().fromJson(content, VedioEntity.class);
            int vedioSize = vedioEntity.getVedioSize();
            String getVedioBitmapUrl = vedioEntity.getGetVedioBitmapUrl();
            SimpleDateFormat formatter = new SimpleDateFormat("mm:ss");
            String format = formatter.format(vedioSize);
            holder.tvTime.setText(format);
            GlideUtils.loadImage(context, getVedioBitmapUrl, holder.imgVideo);
            /*holder.imgVideo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(context, PlayActivity.class);
                    intent.putExtra("videoUrl",vedioEntity.getVedioUrl());
                    context.startActivity(intent);
                }
            });
            holder.imgVideo.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View view) {
                    onlongClickListenler.longClick(holder.imgVideo,position);
                    return true;
                }
            });*/
        } catch (Exception e) {

        }
    }

    private void setImglayout(final ColatImgViewHolder holder, final String content, final int position) {

        String imageUrl = IMMessageUtil.getImageUrl(content);

        if (imageUrl != null && imageUrl.contains(".gif")) {
            Glide.with(context).load(imageUrl).asGif().diskCacheStrategy(DiskCacheStrategy.NONE).into(holder.imageView);
        } else {
            Glide.with(context).load(imageUrl).into(holder.imageView);
        }
       /* holder.linearLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                List<String> imgs = new ArrayList<String>();
                imgs.add(content);
                ImagePagerActivity.ImageSize imageSize = new ImagePagerActivity.ImageSize(view.getMeasuredWidth(), view.getMeasuredHeight());

                ImagePagerActivity.startImagePagerActivity(((CollectionActivity) context), imgs,0, imageSize);

            }
        });*/
        /*holder.linearLayout.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {
                onlongClickListenler.longClick(holder.linearLayout,position);
                return true;
            }
        });*/
    }

    private void setTextlayout(final RecyclerView.ViewHolder holder, String content, final int position, final View backview) {
        ((ColatTextViewHolder) holder).bqmmMessageText.setClickable(false);

       /* ((ColatTextViewHolder)holder).bqmmMessageText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               backview.setPressed(true);
            }
        });
        ((ColatTextViewHolder)holder).bqmmMessageText.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                backview.setPressed(true);
                onlongClickListenler.longClick(backview,position);
                return true;
            }
        });*/
        try {
            MsgEntity msgEntity = new Gson().fromJson(content, MsgEntity.class);
            if (msgEntity.getMsgCodes() != null && !"".equals(msgEntity.getMsgCodes())) {
                //大表情
                try {
                    JSONArray array = new JSONArray(msgEntity.getMsgCodes());
                    LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
                    params.setMargins(0, 0, ScreenUtil.dp2px(10), 0);
                    ((ColatTextViewHolder) holder).bqmmMessageText.setLayoutParams(params);
                    ((ColatTextViewHolder) holder).bqmmMessageText.setBackgroundResource(R.color.chat_backgroud);
                    ((ColatTextViewHolder) holder).bqmmMessageText.showMessage(msgEntity.getMsgString(), msgEntity.getMsgType(), array);
                    ((ColatTextViewHolder) holder).bqmmMessageText.setBigEmojiShowSize(ScreenUtil.dp2px(100));
                    // Log.i("info","iiii=="+array);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                //图文混排
                ((ColatTextViewHolder) holder).bqmmMessageText.setText(msgEntity.getMsgString());
            }

        } catch (Exception e) {
            ((ColatTextViewHolder) holder).bqmmMessageText.setText(content);
        }
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public int getItemViewType(int position) {
        if (null != datas && datas.get(position) != null) {
            return Integer.parseInt(datas.get(position).getCategory());
        }
        return super.getItemViewType(position);
    }

    public interface OnlongClickListenler {
        void longClick(View view, int position);
    }

    public interface OnCollecClickListenler {
        void onClick(View view, int position);
    }
}
